/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui.controls;

import ac.lib.tools.files;
import java.awt.Dimension;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import tmp.ac.options.urls;

/**
 *
 * @author empcl_000
 */
public class PCLImage extends JLabel {

    files f = new files();

    public PCLImage() {

        setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        setSize(new Dimension(100, 100));
        setPreferredSize(new Dimension(100, 100));
        setBackground(new java.awt.Color(153, 153, 153));

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pCLImage1MouseClicked(evt);
            }
        });

        addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                //pCLImage1PropertyChange(evt);
            }
        });

    }

    private void pCLImage1PropertyChange(java.beans.PropertyChangeEvent evt) {
        try {
            try {
                updateImage(this.getClass().getResource(urls.Image_URL + "" + getText()).toURI().toURL(), getText());
            } catch (URISyntaxException ex) {
                Logger.getLogger(PCLImage.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(PCLImage.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void pCLImage1MouseClicked(java.awt.event.MouseEvent evt) {

        f.getImage(this);
        updateImage(f.url, f.FileName);

    }

    public String getImageB() {

        String u = "" + f.url;
        String r = u.replace("file:/", "");

        return r;
    }

    public String getImageUrl() {
        String r = "";
        if (f.url != null) {

            String u = "" + f.url;
            r = u.replace("file:/", "");
        }
        return r;
    }

    public void getImage() {
    }

    public void updateImage(URL utl, String label) {

        setText(label);
        ImageIcon imageIcon = new ImageIcon(utl);
        ImageIcon thumbnailIcon = new ImageIcon(new tmp.ac.lib.tools.files().getScaledImage(imageIcon.getImage(), 225, 180));
        setIcon(thumbnailIcon);
    }

    public void updateImage(ImageIcon imageIcon, String label) {

     /*   Image img = imageIcon.getImage();
        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        
        
        g.drawImage(img, 0, 0, this.getPreferredSize().width, this.getPreferredSize().height, null);
        imageIcon= new ImageIcon(bi);
*/
        setText(label);

        ImageIcon thumbnailIcon = new ImageIcon(new tmp.ac.lib.tools.files().getScaledImage(imageIcon.getImage(), this.getPreferredSize().width, this.getPreferredSize().height));
        setIcon(thumbnailIcon);
    }

}
